package com.sfdk.core {
    import com.sfdk.core.abstract.AbstractState;
    import com.sfdk.data.RectangleID;
    import com.sfdk.data.SoundEx;
    import com.sfdk.data.WaveSoundEx;
    import com.sfdk.graphics.core.BitmapEx;
    import com.sfdk.graphics.core.CursorManager;
    import com.sfdk.graphics.core.DebugWindow;
    import com.sfdk.graphics.core.SpriteEx;
    import com.sfdk.log.Logger;
    import com.sfdk.net.php.RemotingService;
    import com.sfdk.pools.AnimatedBitmapPool;
    import com.sfdk.pools.BitmapDataPool;
    import com.sfdk.pools.LayeredBitmapPool;
    import com.sfdk.pools.MusicPool;
    import com.sfdk.pools.TextFormatPool;
    import com.sfdk.pools.WaveSoundPool;

    import flash.events.Event;
    import flash.geom.Point;
    import flash.net.registerClassAlias;
    import flash.system.Capabilities;

    BitmapEx;
    SoundEx;
    WaveSoundEx;

    public class GameProxy extends SpriteEx {
        private var _currentState:AbstractState;
        private var _logger:Logger;
        private var _dataStore:DataStore;
        private var _remoteService:RemotingService;

        private var _debugWindow:DebugWindow;

        public function GameProxy() {
            _logger = new Logger();
            _dataStore = new DataStore();
            new TextFormatPool();
            new BitmapDataPool();
            new AnimatedBitmapPool();
            new LayeredBitmapPool();
            new BitmapDataPool();
            new MusicPool();
            new WaveSoundPool();
            new CursorManager(this);

            registerClassAlias("com.sfdk.data.RectangleID", RectangleID);
            registerClassAlias("flash.geom.Point", Point);

            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage, false, 0, true);
        }

        private function onAddedToStage(e:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
            new KeyboardListener(this);

            // Init some configuration data.
            GameUtil.STAGE_WIDTH = stage.stageWidth;
            GameUtil.STAGE_HEIGHT = stage.stageHeight;

            if (Capabilities.isDebugger) {
                _debugWindow = new DebugWindow();
                KeyboardListener.instance.addEventListener("@", onDebugKeyPressed);
            }
        }

        public function set remoteService(value:RemotingService):void {
            _remoteService = value;
        }

        public function get remoteService():RemotingService {
            return _remoteService;
        }

        public function getState():AbstractState {
            return _currentState;
        }

        public function setState(newStateClass:Class, parameters:Object):void {
            // First we force the current state to end;
            if (_currentState != null)
                _currentState.endState();

            _currentState = new newStateClass(this, parameters) as AbstractState;
            if (_currentState == null)
                Logger.error("GameProxy :: Cannot instantiate the new state");
        }


        private function onDebugKeyPressed(e:Event):void {
            if (contains(_debugWindow))
                removeChild(_debugWindow);
            else
                attachChild(_debugWindow, 0, 0, "DebugWindow");
        }

        public function stateLoaded():void {

        }
    }
}